package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.resource.ResourceQueryRequest;
import com.gr.smartedubackend.model.entity.Resource;
import com.gr.smartedubackend.service.ResourceService;
import com.gr.smartedubackend.mapper.ResourceMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
* @author 尘轩
* @description 针对表【resource(资源)】的数据库操作Service实现
* @createDate 2025-07-04 08:21:33
*/
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource>
    implements ResourceService{

    @Override
    public QueryWrapper<Resource> getQueryWrapper(ResourceQueryRequest resourceQueryRequest) {

        ThrowUtils.throwIf(resourceQueryRequest == null, ErrorCode.PARAMS_ERROR);

        String resourceName = resourceQueryRequest.getResourceName();
        String resourceType = resourceQueryRequest.getResourceType();
        String keyword = resourceQueryRequest.getKeyword();
        String uploaderName = resourceQueryRequest.getUploaderName();
        String sortField = resourceQueryRequest.getSortField();
        String sortOrder = resourceQueryRequest.getSortOrder();

        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(resourceName), "resourceName", resourceName);
        queryWrapper.like(StrUtil.isNotEmpty(resourceType), "resourceType", resourceType);
        queryWrapper.like(StrUtil.isNotEmpty(keyword), "description", keyword);
        queryWrapper.like(StrUtil.isNotEmpty(uploaderName), "uploaderName", uploaderName);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




